package sign

import (
	"github.com/bighuangbee/gokit/tools"
	"sort"
	"strings"
)

const SignKey = "sign"
const CryptoKey = "dad52b5719e3202e32a6619e14d0ccec"

func CreateSign(cryptoKey string, kvs map[string]interface{})(string){
	var keys []string
	for key,_ := range kvs{
		keys = append(keys, key)
	}
	sort.Strings(keys)

	var signStr string
	for _, k := range keys{
		val := strings.TrimRight(strings.TrimLeft(tools.ToStr(kvs[k]), "[\""), "\"]")
		if k != SignKey {
			signStr += k + "=" + val + "&"
		}
	}

	signStr = strings.TrimRight(signStr, "&")
	return tools.MD5(cryptoKey + tools.MD5(signStr))
}

func SignCheck(cryptoKey string, kvs map[string]interface{})bool{
	sign := CreateSign(cryptoKey, kvs)
	if ClientStr, ok := kvs[SignKey]; ok{
		if sign == ClientStr{
			return true
		}
	}
	return false
}
